home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / MPW / indent 1.8 / backup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-19  |  9.1 KB  |  379 lines  |  [TEXT/MPS ]

  1. /* backup.c -- make Emacs style backup file names
  2.    Copyright (C) 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it without restriction.
  6.  
  7.    This program is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10.  
  11.  
  12.  
  13.    GNU/Emacs style backups --
  14.    This behaviour is controlled by two environment variables,
  15.    VERSION_CONTROL and SIMPLE_BACKUP_SUFFIX.
  16.  
  17.    VERSION_CONTROL determines what kinds of backups are made.  If it's
  18.    value is "numbered", then the first modification of some file
  19.    "eraserhead.c" will yield a backup file "eraserhead.c.~1~", the
  20.    second modification will yield "eraserhead.c.~2~", and so on.  It
  21.    does not matter if the version numbers are not a sequence;  the next
  22.    version will be one greater than the highest in that directory.
  23.  
  24.    If the value of VERSION_CONTROL is "numbered_existing", then such
  25.    numbered backups will be made if there are already numbered backup
  26.    versions of the file.  Otherwise, the backup name will be that of
  27.    the original file with "~" (tilde) appended.  E.g., "eraserhead.c~".
  28.  
  29.    If the value of VERSION_CONTROL is "simple", then the backup name
  30.    will be that of the original file with "~" appended, regardless of
  31.    whether or not there exist numbered versions in the directory.
  32.  
  33.    For simple backups, the value of SIMPLE_BACKUP_SUFFIX will be used
  34.    rather than "~" if it is set.
  35.  
  36.    If VERSION_CONTROL is unset, "numbered_existing" is assumed.  For
  37.    Emacs lovers, "nil" is equivalent to "numbered_existing" and "t" is
  38.    equivalent to "numbered".
  39.  
  40.    Finally, if VERSION_CONTROL is "none" or "never", backups are not
  41.    made.  I suggest you avoid this behaviour. */
  42.  
  43. /* Written by jla, based on code from djm (see `patch') */
  44.  
  45. #include "sys.h"
  46. #include "backup.h"
  47. #include <ctype.h>
  48.  
  49. #ifndef isascii
  50. #define ISDIGIT(c) (isdigit ((unsigned char) (c)))
  51. #else
  52. #define ISDIGIT(c) (isascii (c) && isdigit (c))
  53. #endif
  54.  
  55. #ifndef NODIR
  56.  
  57. #include <sys/types.h>
  58.  
  59. #ifdef DIRENT
  60. #include <dirent.h>
  61. #ifdef direct
  62. #undef direct
  63. #endif
  64. #define direct dirent
  65. #define NLENGTH(direct) (strlen((direct)->d_name))
  66. #else /* !DIRENT */
  67. #define NLENGTH(direct) ((direct)->d_namlen)
  68. #ifdef USG
  69. #ifdef SYSNDIR
  70. #include <sys/ndir.h>
  71. #else /* !SYSNDIR */
  72. #include <ndir.h>
  73. #endif /* !SYSNDIR */
  74. #else /* !USG */
  75. #include <sys/dir.h>
  76. #endif /* !USG */
  77. #endif /* !DIRENT */
  78.  
  79. #if defined (HAVE_UNISTD_H)
  80. #include <unistd.h>
  81. #endif
  82.  
  83. #if defined (_POSIX_VERSION)    /* Might be defined in unistd.h.  */
  84. /* POSIX does not require that the d_ino field be present, and some
  85.    systems do not provide it. */
  86. #define REAL_DIR_ENTRY(dp) 1
  87. #else
  88. #define REAL_DIR_ENTRY(dp) ((dp)->d_ino != 0)
  89. #endif
  90.  
  91. #else /* NODIR */
  92. #define generate_backup_filename(v,f) simple_backup_name((f))
  93. #endif /* NODIR */
  94.  
  95. #ifndef SYS_BACKUP_SUFFIX
  96. #define SYS_BACKUP_SUFFIX "~"
  97. #endif
  98.  
  99. #ifndef BACKUP_SUFFIX_STR
  100. #define BACKUP_SUFFIX_STR    "~"
  101. #endif
  102.  
  103. #ifndef BACKUP_SUFFIX_CHAR
  104. #define BACKUP_SUFFIX_CHAR   '~'
  105. #endif
  106.  
  107. #ifndef BACKUP_SUFFIX_FORMAT
  108. #define BACKUP_SUFFIX_FORMAT "%s.~%d~"
  109. #endif
  110.  
  111.  
  112. /* Default backup file suffix to use */
  113. static char *simple_backup_suffix = SYS_BACKUP_SUFFIX;
  114.  
  115. /* What kinds of backup files to make -- see
  116.    table `version_control_values' below. */
  117. enum backup_mode version_control = unknown;
  118.  
  119.  
  120. /* Construct a simple backup name for PATHNAME by appending
  121.    the value of `simple_backup_suffix'. */
  122.  
  123. static char *
  124. simple_backup_name (pathname)
  125.      char *pathname;
  126. {
  127.   char *backup_name;
  128.  
  129.   backup_name = xmalloc (strlen (pathname)
  130.              + strlen (simple_backup_suffix) + 2);
  131.   sprintf (backup_name, "%s%s", pathname, simple_backup_suffix);
  132.   return backup_name;
  133. }
  134.  
  135. #ifndef NODIR
  136. /* If DIRENTRY is a numbered backup version of file BASE, return
  137.    that number.  BASE_LENGTH is the string length of BASE. */
  138.  
  139. static int
  140. version_number (base, direntry, base_length)
  141.      char *base;
  142.      char *direntry;
  143.      int base_length;
  144. {
  145.   int version;
  146.   char *p;
  147.  
  148.   version = 0;
  149.   if (!strncmp (base, direntry, base_length)
  150.       && ISDIGIT (direntry[base_length + 2]))
  151.     {
  152.       for (p = &direntry[base_length + 2]; ISDIGIT (*p); ++p)
  153.     version = version * 10 + *p - '0';
  154.       if (p[0] != BACKUP_SUFFIX_CHAR || p[1])
  155.     version = 0;
  156.     }
  157.  
  158.   return version;
  159. }
  160.  
  161.  
  162. /* Return the highest version of file FILENAME in directory
  163.    DIRNAME.  Return 0 if there are no numbered versions. */
  164.  
  165. static int
  166. highest_version (filename, dirname)
  167.      char *filename, *dirname;
  168. {
  169.   DIR *dirp;
  170.   struct direct *dp;
  171.   int highest_version;
  172.   int this_version;
  173.   int file_name_length;
  174.  
  175.   dirp = opendir (dirname);
  176.   if (!dirp)
  177.     return 0;
  178.  
  179.   highest_version = 0;
  180.   file_name_length = strlen (filename);
  181.  
  182.   while ((dp = readdir (dirp)) != 0)
  183.     {
  184.       if (!REAL_DIR_ENTRY (dp) || NLENGTH (dp) <= file_name_length + 2)
  185.     continue;
  186.  
  187.       this_version = version_number (filename, dp->d_name, file_name_length);
  188.       if (this_version > highest_version)
  189.     highest_version = this_version;
  190.     }
  191.  
  192.   closedir (dirp);
  193.   return highest_version;
  194. }
  195.  
  196.  
  197. /* Return the highest version number for file PATHNAME.  If there
  198.    are no backups, or only a simple backup, return 0. */
  199.  
  200. static int
  201. max_version (pathname)
  202.      char *pathname;
  203. {
  204.   register char *p;
  205.   register char *filename;
  206.   int pathlen = strlen (pathname);
  207.   int version;
  208.  
  209.   p = pathname + pathlen - 1;
  210. #ifdef macintosh
  211.   while (p > pathname && *p != ':')
  212.     p--;
  213.   if (*p == ':')
  214. #else
  215.   while (p > pathname && *p != '/')
  216.     p--;
  217.   if (*p == '/')
  218. #endif
  219.     {
  220.       int dirlen = p - pathname;
  221.       register char *dirname;
  222.  
  223.       filename = p + 1;
  224.       dirname = xmalloc (dirlen + 1);
  225.       strncpy (dirname, pathname, (dirlen));
  226.       dirname[dirlen] = '\0';
  227.       version = highest_version (filename, dirname);
  228.       free (dirname);
  229.       return version;
  230.     }
  231.  
  232.   filename = pathname;
  233. #ifdef macintosh
  234.   version = highest_version (filename, ":");
  235. #else
  236.   version = highest_version (filename, ".");
  237. #endif
  238.   return version;
  239. }
  240.  
  241.  
  242. /* Generate a backup filename for PATHNAME, dependent on the
  243.    value of VERSION_CONTROL. */
  244.  
  245. static char *
  246. generate_backup_filename (version_control, pathname)
  247.      enum backup_mode version_control;
  248.      char *pathname;
  249. {
  250.   int last_numbered_version;
  251.   char *backup_name;
  252.  
  253.   if (version_control == none)
  254.     return 0;
  255.  
  256.   if (version_control == simple)
  257.     return simple_backup_name (pathname);
  258.  
  259.   last_numbered_version = max_version (pathname);
  260.   if (version_control == numbered_existing
  261.       && last_numbered_version == 0)
  262.     return simple_backup_name (pathname);
  263.  
  264.   last_numbered_version++;
  265.   backup_name = xmalloc (strlen (pathname) + 16);
  266.   if (!backup_name)
  267.     return 0;
  268.  
  269.   sprintf (backup_name, BACKUP_SUFFIX_STR, pathname,
  270.        (int) last_numbered_version);
  271.   return backup_name;
  272. }
  273.  
  274. #endif /* !NODIR */
  275.  
  276. static struct version_control_values values[] =
  277. {
  278.   {none, "never"},        /* Don't make backups. */
  279.   {simple, "simple"},        /* Only simple backups */
  280.   {numbered_existing, "existing"},    /* Numbered if they already exist */
  281.   {numbered_existing, "nil"},    /* Ditto */
  282.   {numbered, "numbered"},    /* Numbered backups */
  283.   {numbered, "t"},        /* Ditto */
  284.   {unknown, 0}            /* Initial, undefined value. */
  285. };
  286.  
  287.  
  288. extern char *getenv ();
  289.  
  290. /* Determine the value of `version_control' by looking in the
  291.    environment variable "VERSION_CONTROL".  Defaults to
  292.    numbered_existing. */
  293.  
  294. enum backup_mode
  295. version_control_value ()
  296. {
  297.   char *version;
  298.   struct version_control_values *v;
  299.  
  300.   version = getenv ("VERSION_CONTROL");
  301.   if (version == 0 || *version == 0)
  302.     return numbered_existing;
  303.  
  304.   v = &values[0];
  305.   while (v->name)
  306.     {
  307.       if (strcmp (version, v->name) == 0)
  308.     return v->value;
  309.       v++;
  310.     }
  311.  
  312.   return unknown;
  313. }
  314.  
  315.  
  316. /* Initialize information used in determining backup filenames. */
  317.  
  318. void
  319. initialize_backups ()
  320. {
  321.   char *v = getenv ("SIMPLE_BACKUP_SUFFIX");
  322.  
  323.   if (v && *v)
  324.     simple_backup_suffix = v;
  325. #ifdef NODIR
  326.   version_control = simple;
  327. #else /* !NODIR */
  328.   version_control = version_control_value ();
  329.   if (version_control == unknown)
  330.     {
  331.       fprintf (stderr, "indent:  Strange version-control value\n");
  332.       fprintf (stderr, "indent:  Using numbered-existing\n");
  333. #ifdef MPW
  334.       (void) fflush (stderr);
  335. #endif
  336.       version_control = numbered_existing;
  337.     }
  338. #endif /* !NODIR */
  339. }
  340.  
  341. /* Prints an error message using `perror' */
  342. extern void sys_error ();
  343.  
  344. /* Make a backup copy of FILE, taking into account version-control.
  345.    See the description at the beginning of the file for details. */
  346.  
  347. void
  348. make_backup (file)
  349.      struct file_buffer *file;
  350. {
  351.   int fd;
  352.   register char *p = file->name + strlen (file->name) - 1;
  353.   char *backup_filename;
  354.   char *new_backup_name;
  355.  
  356.   backup_filename = generate_backup_filename (version_control, file->name);
  357.   if (!backup_filename)
  358.     {
  359.       fprintf (stderr, "indent: Can't make backup filename of %s", file->name);
  360.       exit (1);
  361.     }
  362.  
  363. #ifdef macintosh
  364.   fd = creat (backup_filename);
  365. #else
  366.   fd = creat (backup_filename, 0666);
  367. #endif
  368.   if (fd < 0)
  369.     sys_error (backup_filename);
  370.   if (write (fd, file->data, file->size) != file->size)
  371.     sys_error (backup_filename);
  372. #ifdef MPW
  373.   fsetfileinfo (backup_filename, 'MPS ', 'TEXT');
  374. #endif
  375.  
  376.   close (fd);
  377.   free (backup_filename);
  378. }
  379.